package yyy.ab.modules.security;

import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import yyy.ab.modules.security.dto.SecurityUserDto;
import yyy.ab.modules.system.entity.SysUser;
import yyy.ab.modules.system.service.SysUserService;

import java.util.ArrayList;

@Component
@RequiredArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {

    private final SysUserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        SecurityUserDto securityUser = null;
        SysUser user = userService.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("不存在该用户!");
        }
        securityUser = new SecurityUserDto(user.getId(), user.getUsername(), user.getPassword(), new ArrayList<>());
        return securityUser;
    }
}
